package com.shop.cereshop.agent.userAuth;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Util {

    public static String md5(String input) {
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            byte[] bytes = digest.digest(input.getBytes("UTF-8"));
            StringBuilder sb = new StringBuilder();
            for (byte b : bytes) {
                sb.append(String.format("%02x", b));
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException | java.io.UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    // 示例：加密身份证号
    public static String encryptIdCard(String idCard) {
        if (idCard == null || idCard.length() < 18) {
            return idCard;
        }
        String first6 = idCard.substring(0, 6);
        String middle = idCard.substring(6, 14); // 出生年月日
        String last4 = idCard.substring(14);
        String md5Part = md5(middle);
        return first6 + md5Part + last4;
    }

    // 示例：加密姓名
    public static String encryptName(String name) {
        if (name == null || name.length() == 0) {
            return name;
        }
        String firstChar = name.substring(0, 1);
        String rest = name.substring(1);
        return md5(firstChar) + rest;
    }

    public static void main(String[] args) {
        System.out.println(encryptIdCard("429001********8211")); // 输出: 429001...8211
        System.out.println(encryptName("张三")); // 输出: 6499fc...三
    }
}

